package com.mzpai.util;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.gexin.fastjson.JSONArray;
import com.gexin.fastjson.JSONObject;

public class List2JSON<T> {
	/**
	 * 方法重写
	 * @param page
	 * @param count
	 * @param total
	 * @param datas
	 * @param showCellNames
	 * @return
	 * @throws Exception
	 */
	public JSONObject toJSON(int page, long count, int total, List<T> datas,  String[] showCellNames) throws Exception {
		JSONObject json = new JSONObject();
		json.put("page", page);
		json.put("total", count);
		json.put("records", total);
		JSONArray rows = new JSONArray();
		for (T t : datas) {
			JSONObject row = new JSONObject();
			Map<String, Object> params = new HashMap<String, Object>();
			StringUtils.convertParams(t, params);
			for (String fieldName : showCellNames) {
				row.put(fieldName, params.get(fieldName));
			}
			/**
			row.put("id", params.get(hostCellName));
			for (String fieldName : showCellNames) {
				cell.put(params.get(fieldName));
			}
			**/
			rows.add(row);
		}
		json.put("rows", rows);
		return json;
	}
	
	/**
	 * list 转化
	 * @param datas
	 * @param showCellNames
	 * @return
	 * @throws Exception
	 */
	public String listToJson(List<T> datas,  String[] showCellNames)
			 throws Exception {
		 JSONArray rows = new JSONArray();
		 for (T t : datas) {
			 JSONObject row = new JSONObject();
			 Map<String, Object> params = new HashMap<String, Object>();
			 StringUtils.convertParams(t, params);
			 for (String fieldName : showCellNames) {
			 	row.put(fieldName, params.get(fieldName));
			 }
			 rows.add(row);
		 }
		 return rows.toString();
	 }

	public static void main(String[] args) {
		List<String> a = new ArrayList<String>();
		a.add("1");
		a.add("2");
		a.add("3");
		a.add("5");
		List<String> b = new ArrayList<String>();
		b.add("2");
		b.add("3");
		b.add("4");
		List<String> c = b.stream().filter(item -> a.contains(item)).collect(Collectors.toList());
		List<String> d = b.stream().filter(item -> !a.contains(item)).collect(Collectors.toList());
		a.addAll(d);
		System.out.println(a);
		
	}
}
